#include "widget.h"
#include "ui_widget.h"
#include <QMouseEvent>
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    //qDebug() << 1;
    ui->setupUi(this);
    ui->stackedWidget->setCurrentWidget(ui->page_12);
    map.load(":/new/prefix1/2ad11b094c93197d-4afaf786506af54b-9fd277aea03c8387ebd35dcfba4ba3dd.jpg");
    ui->a_label_picture5->setPixmap(map);
    ui->a_label_picture5->setScaledContents(true);
    ui->a_picture_page_land->setPixmap(map);
    ui->a_picture_page_land->setScaledContents(true);
    ui->label_picture1->setPixmap(map);
    ui->label_picture1->setScaledContents(true);
    ui->label_picture2->setPixmap(map);
    ui->label_picture2->setScaledContents(true);
    flag = 0;
    //ui->textEdit->setf;

    pic = new paint;
    connect(pic,SIGNAL(set_chartView_signal()),this,SLOT(set_chartView_slots()));
    connect(pic,SIGNAL(set_chartView_2_signal()),this,SLOT(set_chartView_2_slots()));
    connect(pic,SIGNAL(set_chartView_3_signal()),this,SLOT(set_chartView_3_slots()));

    /*设置画笔和画布*/
    pic->chart = ui->chartView->chart();
    pic->chart->setTitle(QString("参数变化曲线图"));//设置图标标题
    ui->chartView->setRenderHint(QPainter::Antialiasing); //设置抗锯齿
//    ui->chartView->resize(400, 400);   //画布大小
    pic->chart->addSeries(pic->series0);
    pic->chart->addSeries(pic->series1);
    pic->chart->addSeries(pic->series2);

    pic->chart_month = ui->chartView_2->chart();
    pic->chart_month->setTitle(QString("月参数变化曲线图"));//设置图标标题
    pic->chart_month->addSeries(pic->series0_month);
    pic->chart_month->addSeries(pic->series1_month);
    pic->chart_month->addSeries(pic->series2_month);
    ui->chartView_2->setVisible(false);


    pic->chart_week = ui->chartView_3->chart();
    pic->chart_week->setTitle(QString("周参数变化曲线图"));//设置图标标题
    pic->chart_week->addSeries(pic->series0_week);
    pic->chart_week->addSeries(pic->series1_week);
    pic->chart_week->addSeries(pic->series2_week);
    ui->chartView_3->setVisible(false);
    /*绘制坐标轴*/
    //当天
    pic->paint_x_axes();
    pic->paint_y_axes();
    pic->paint_y_lux_day_axes();
    //当月
    pic->paint_month_x_axes();
    pic->paint_month_y_axes();
    pic->paint_y_lux_month_axes();
    //当周
    pic->paint_week_x_axes();
    pic->paint_week_y_axes();
    pic->paint_y_lux_week_axes();
    //实时画当天的
    pic->paint_picture_slot();
    pic->timer.start(10000);//10s获取一次数据
    ui->chartView->setRenderHint(QPainter::Antialiasing); //设置抗锯齿

    connect(&timer,SIGNAL(timeout()),this,SLOT(freshdata_slot()));
    timer.start(100);

    //设置手动/自动切换按钮
    ui->pushButton_auto->setText("自动");
    ui->airFan_open_2->setEnabled(false);
    ui->sunshade_open_2->setEnabled(false);
    ui->sprayWater_open_2->setEnabled(false);
    //视频线程
    VDthread = new videothread;

//    connect(VDthread, SIGNAL(Transmission(QString)), this, SLOT(DisplayMsgslot(QString)));

    connect(VDthread,SIGNAL(display_movie_signals()),this,SLOT(DisplayMovieSlot()));
    /*ui->setupUi(this);
    QVBoxLayout *mainlayout = new QVBoxLayout;
    mainlayout->addWidget(ui->stackedWidget);
    setLayout(mainlayout);

    ui->stackedWidget->resize(this->size());
    ui->widget->resize(this->size());
    ui->widget1->resize(this->size());
    ui->widget2->resize(this->size());*/
}

Widget::~Widget()
{
    delete ui;
    delete pic;
    delete VDthread;
}
//设置图形到chartView
void Widget::set_chartView_slots()
{
    ui->chartView->setChart(pic->chart);
}
void Widget::set_chartView_2_slots()
{
    ui->chartView_2->setChart(pic->chart_month);
}
void Widget::set_chartView_3_slots()
{
    ui->chartView_3->setChart(pic->chart_week);
}

//点击查看折线图button
void Widget::on_to_page11_clicked()
{
    qDebug() << op_ip;
    ui->stackedWidget->setCurrentWidget(ui->page_land);
}

void Widget::freshdata_slot()
{
    if(NULL != maxmin.max_env.tem)
    {
        ui->label_temp_set_max->setText(QString("%1").arg(maxmin.max_env.tem, 0, 'f', 1));
        ui->label_temp_set_min->setText(QString("%1").arg(maxmin.min_env.tem, 0, 'f', 1));
        ui->label_ilum_set_max->setText(QString("%1").arg(maxmin.max_env.hum, 0, 'f', 1));
        ui->label_ilum_set_min->setText(QString("%1").arg(maxmin.min_env.hum, 0, 'f', 1));
        ui->label_humi_set_max->setText(QString("%1").arg(maxmin.max_env.lux, 0, 'f', 1));
        ui->label_humi_set_min->setText(QString("%1").arg(maxmin.min_env.lux, 0, 'f', 1));

        ui->label_current_temp_max->setText(QString("%1").arg(maxmin.max_env.tem, 0, 'f', 1));
        ui->label_current_temp_min->setText(QString("%1").arg(maxmin.min_env.tem, 0, 'f', 1));
        ui->label_current_ilum_max->setText(QString("%1").arg(maxmin.max_env.hum, 0, 'f', 1));
        ui->label_current_ilum_min->setText(QString("%1").arg(maxmin.min_env.hum, 0, 'f', 1));
        ui->label_current_humi_max->setText(QString("%1").arg(maxmin.max_env.lux, 0, 'f', 1));
        ui->label_current_humi_min->setText(QString("%1").arg(maxmin.min_env.lux, 0, 'f', 1));
    }

    ui->num_temp_2->setText(QString("%1").arg(from_server.env_data.tem, 0, 'f', 1));
    ui->num_humi_3->setText(QString("%1").arg(from_server.env_data.hum, 0, 'f', 1));
    ui->num_ilum_3->setText(QString("%1").arg(from_server.env_data.lux, 0, 'f', 1));

    if(1 == dev.air_fan)
    {
        ui->airFan_state_2->setText("已启动");
        ui->airFan_state_2->setStyleSheet("color:red");
        ui->airFan_open_2->setText("关闭风扇");
    }
    else if(0 == dev.air_fan)
    {
        ui->airFan_state_2->setText("未启动");
        ui->airFan_state_2->setStyleSheet("color:black");
        ui->airFan_open_2->setText("打开风扇");
    }

    if(1 == dev.sunshade)
    {
        ui->shadler_state_2->setText("已启动");
        ui->shadler_state_2->setStyleSheet("color:red");
        ui->sunshade_open_2->setText("关闭遮阳");
    }
    else if(0 == dev.sunshade)
    {
        ui->shadler_state_2->setText("未启动");
        ui->shadler_state_2->setStyleSheet("color:black");
        ui->sunshade_open_2->setText("打开遮阳");
    }

    if(1 == dev.spray_water)
    {
        ui->sprayWater_state_2->setText("已启动");
        ui->sprayWater_state_2->setStyleSheet("color:red");
        ui->sprayWater_open_2->setText("关闭喷水");
    }
    else if(0 == dev.spray_water)
    {
        ui->sprayWater_state_2->setText("未启动");
        ui->sprayWater_state_2->setStyleSheet("color:black");
        ui->sprayWater_open_2->setText("打开喷水");
    }

    switch(from_server.env_state.tem_state)
    {
    case NORMAL:
        ui->num_temp_2->setStyleSheet("color:green");
        ui->unit_temp_2->setStyleSheet("color:green");
        break;
    case LOW:
        ui->num_temp_2->setStyleSheet("color:blue");
        ui->unit_temp_2->setStyleSheet("color:blue");
        break;
    case HEIGHT:
        ui->num_temp_2->setStyleSheet("color:red");
        ui->unit_temp_2->setStyleSheet("color:red");
        break;
    }

    switch(from_server.env_state.hum_state)
    {
    case NORMAL:
        ui->num_humi_3->setStyleSheet("color:green");
        ui->unit_humi_2->setStyleSheet("color:green");
        break;
    case LOW:
        ui->num_humi_3->setStyleSheet("color:red");
        ui->unit_humi_2->setStyleSheet("color:red");
        break;
    case HEIGHT:      
        ui->num_humi_3->setStyleSheet("color:blue");
        ui->unit_humi_2->setStyleSheet("color:blue");
        break;
    }

    switch(from_server.env_state.lux_state)
    {
    case NORMAL:
        ui->num_ilum_3->setStyleSheet("color:green");
        ui->unit_ilum_2->setStyleSheet("color:green");
        break;
    case LOW:
        ui->num_ilum_3->setStyleSheet("color:blue");
        ui->unit_ilum_2->setStyleSheet("color:blue");
        break;
    case HEIGHT:
        ui->num_ilum_3->setStyleSheet("color:red");
        ui->unit_ilum_2->setStyleSheet("color:red");
        break;
    }
}

void Widget::on_to_page13_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->page_13);
    VDthread->run();
//    VDthread->start();
}

void Widget::on_pushButton_return_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->page_12);
    VDthread->msdisconnected();
}

void Widget::on_return_pg12_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->page_12);
}

void Widget::on_back_to_page12_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->page_12);
}

void Widget::sendmaxmin()
{
    memset(send_buf, 0, FLAG_SIZE);
    memcpy(send_buf, "#PZXG#", 6);
    memcpy(send_buf + 6, &maxmin, sizeof(config));
    int ret = sockvector.at(op_ip)->write(send_buf, FLAG_SIZE);
    qDebug() << ret;
}

void Widget::sendadmin()
{
    memset(send_buf, 0, FLAG_SIZE);
    memcpy(send_buf, "#DEVI#", 6);
    memcpy(send_buf + 6, &dev, sizeof(dev1));
    int ret = sockvector.at(op_ip)->write(send_buf, FLAG_SIZE);
    qDebug() << ret;
}

void Widget::on_save_in_page11_clicked()
{
    if( ui->lineEdit_humi_max_set->text().toFloat() < ui->lineEdit_humi_min_set->text().toFloat() ||
            ui->lineEdit_temp_max_set->text().toFloat() < ui->lineEdit_temp_min_set->text().toFloat() ||
            ui->lineEdit_ilum_max_set->text().toFloat() < ui->lineEdit_ilum_min_set->text().toFloat())
    {
        QMessageBox::warning(this, "警告", "最小值大于最大值，请重新输入！");
        return;
    }

    memset(&maxmin, 0, FLAG_SIZE);
    maxmin.max_env.tem = ui->lineEdit_temp_max_set->text().toFloat();
    maxmin.max_env.hum = ui->lineEdit_humi_max_set->text().toFloat();
    maxmin.max_env.lux = ui->lineEdit_ilum_max_set->text().toFloat();

    maxmin.min_env.tem = ui->lineEdit_temp_min_set->text().toFloat();
    maxmin.min_env.hum = ui->lineEdit_humi_min_set->text().toFloat();
    maxmin.min_env.lux = ui->lineEdit_ilum_min_set->text().toFloat();

    sendmaxmin();

    ui->save_in_page11->setEnabled(false);
}

void Widget::on_set_option_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->page_11);
    ui->save_in_page11->setEnabled(true);
}

void Widget::on_airFan_open_2_clicked()
{
    if("未启动" == ui->airFan_state_2->text())
    {
        dev.air_fan = 1;
        dev.spray_water = 2;
        dev.sunshade = 2;
        ui->airFan_state_2->setText("已启动");
        ui->airFan_state_2->setStyleSheet("color:red");
        ui->airFan_open_2->setText("关闭风扇");
        sendadmin();
    }
    else {
        dev.air_fan = 0;
        dev.spray_water = 2;
        dev.sunshade = 2;
        ui->airFan_state_2->setText("未启动");
        ui->airFan_state_2->setStyleSheet("color:black");
        ui->airFan_open_2->setText("打开风扇");
        sendadmin();
    }
}

void Widget::on_sunshade_open_2_clicked()
{
    if("未启动" == ui->shadler_state_2->text())
    {
        dev.sunshade = 1;
        dev.air_fan = 2;
        dev.spray_water = 2;
        ui->shadler_state_2->setText("已启动");
        ui->shadler_state_2->setStyleSheet("color:red");
        ui->sunshade_open_2->setText("关闭遮阳");
        sendadmin();
    }
    else {
        dev.sunshade = 0;
        dev.air_fan = 2;
        dev.spray_water = 2;
        ui->shadler_state_2->setText("未启动");
        ui->shadler_state_2->setStyleSheet("color:black");
        ui->sunshade_open_2->setText("打开遮阳");
        sendadmin();
    }
}

//显示月和周的数据折线图
void Widget::on_fllush_clicked()
{
    if( ui->picName->currentText() == "week_month")
    {
        qDebug() << "画整月的数据";
        ui->chartView_3->setVisible(true);   //chartView_3 整周
        ui->chartView_2->setVisible(true);   //chartView_2 整月
        if (bFlag_addSeries != true)
        {
            //发送当前日期给服务器
            QDate date;
            memset(send_buf, 0, FLAG_SIZE);
            if(date.currentDate().month() <  10)
            {
                sprintf(send_buf,"#LSSJ#%d-0%d",date.currentDate().year(),date.currentDate().month());
            }
            else
            {
                sprintf(send_buf,"#LSSJ#%d-%d",date.currentDate().year(),date.currentDate().month());
            }
            send_info_to_server();
        }
        //如果接收到所有的series数据，则调用pic中画当月数据。当周
        else
        {
            pic->get_y_data_month(month_data);
            pic->paint_month_pic();
            pic->get_y_data_week(week_data);
            pic->paint_week_pic();
            month_data.clear();
            week_data.clear();
        }
    }
    else if(ui->picName->currentText() == "day")
    {
        //当天数据
        ui->chartView_3->setVisible(false);
        ui->chartView_2->setVisible(false);
        ui->chartView->setVisible(true);
        qDebug() << "显示当天的数据";
    }
}
void Widget::wheelEvent(QWheelEvent *event)
{
    if (event->delta() > 0) {
        if( ui->picName->currentText() == "day")
        {
            pic->chart->zoom(1.1);
        }
        else if (ui->picName->currentText() == "week_month")
        {
            pic->chart_month->zoom(1.1);
            pic->chart_week->zoom(1.1);
        }
    } else {
        if( ui->picName->currentText() == "day")
        {
             pic->chart->zoom(10.0/11);
        }
        else if (ui->picName->currentText() == "week_month")
        {
            pic->chart_month->zoom(10.0/11);
            pic->chart_week->zoom(10.0/11);
        }
    }
    QWidget::wheelEvent(event);
}

void Widget::on_sprayWater_open_2_clicked()
{
    if("未启动" == ui->sprayWater_state_2->text())
    {
        dev.spray_water = 1 ;
        dev.sunshade = 2;
        dev.air_fan = 2;
        ui->sprayWater_state_2->setText("已启动");
        ui->sprayWater_state_2->setStyleSheet("color:red");
        ui->sprayWater_open_2->setText("关闭喷水");
        sendadmin();
    }
    else {
        dev.spray_water = 0 ;
        dev.sunshade = 2;
        dev.air_fan = 2;
        ui->sprayWater_state_2->setText("未启动");
        ui->sprayWater_state_2->setStyleSheet("color:black");
        ui->sprayWater_open_2->setText("打开喷水");
        sendadmin();
    }
}

void Widget::on_pushButton_clicked()
{
    memset(send_buf, 0, FLAG_SIZE);
    memcpy(send_buf, "#TJKH#", 6);
    sockvector.at(op_ip)->write(send_buf, FLAG_SIZE);
}
//发送消息给服务器
void Widget::send_info_to_server()
{
    qDebug() << send_buf;
    int ret = sockvector.at(op_ip)->write(send_buf, FLAG_SIZE);
    qDebug() << ret;
}

void Widget::mousePressEvent(QMouseEvent *event)
{
    if (event->button() & Qt::LeftButton)
    {
        if (ui->picName->currentText() == "week_month")
        {
            pic->chart_month->scroll(1,0);
            pic->chart_week->scroll(1,0);
        }
        else
        {
            pic->chart->scroll((10.0/360),0);
        }
    }
    else if (event->button() & Qt::RightButton)
    {
        if (ui->picName->currentText() == "week_month")
        {
            pic->chart_month->scroll(-1,0);
            pic->chart_week->scroll(-1,0);
        }
        else
        {
            pic->chart->scroll(-(10.0/360),0);
        }
    }
    else if (event->button() & Qt::MidButton)
    {
        if(ui->picName->currentText() == "week_month")
        {
            pic->chart_week->zoomReset();
            pic->chart_month->zoomReset();
        }
        else
        {
            pic->chart->zoomReset();
        }
    }
    QWidget::mousePressEvent(event);
}

void Widget::mouseReleaseEvent(QMouseEvent *event)
{
    //跳出移动
}

void Widget::on_pushButton_auto_clicked()
{
    if(ui->pushButton_auto->text() == "自动")
    {
        //发送设置模式为手动
        memset(send_buf,0,FLAG_SIZE);
        strcpy(send_buf,"#SDKZ#");
        send_info_to_server();
        //自动 变 手动
        ui->pushButton_auto->setText("手动");
        ui->airFan_open_2->setEnabled(true);
        ui->sunshade_open_2->setEnabled(true);
        ui->sprayWater_open_2->setEnabled(true);
    }
    else
    {
        //手动 变 自动
        memset(send_buf,0,FLAG_SIZE);
        strcpy(send_buf,"#ZDKZ#");
        send_info_to_server();
        //发送设置模式为自动
        ui->pushButton_auto->setText("自动");
        ui->airFan_open_2->setEnabled(false);
        ui->sunshade_open_2->setEnabled(false);
        ui->sprayWater_open_2->setEnabled(false);
    }
}
void Widget::DisplayMovieSlot()
{
    qDebug() << "display on label";
    ui->label_movie->setPixmap(*(VDthread->pixmap));
}
